<?php

namespace Home\Controller;

class DrillController extends HomeCoreController {
	
	public $Model = null;

    protected function _initialize(){
		
        parent::_initialize();
		
        $this->Model = D('Record');
		
    }
	
	public function main(){
		
		$sid = I('get.sid');
		
		$uid = session(C('AUTH_KEYS'));
		
		$mid = session('gid');
		
		$pid = I('get.pid');
		
		$bid = I('get.bid');
		
		$this->assign('bid', $bid);
		
		$nid = I('get.nid');
		
		$this->assign('nid', $nid);
		
		$tid = I('get.tid');
		
		$k = I('get.k');
		
		$id = I('get.id');
		
		if($bid == 'scripts'){
		
			$info = M('Scripts')->where(array('id'=>$sid))->find();
			
			$scripts = $info[$nid];
			
			$map['sid'] = $sid;
				
			$map['uid'] = $uid;
				
			$map['mid'] = $mid;
				
			$map['pid'] = $pid;
			
			if($nid == 'title'){
				
				$this->assign('tid', $info['scriptsname']);
				
				$data['start_time'] = time();
				
				M('Record')->where($map)->save($data);
				
			}elseif($nid == 'department'){
				
				if(!empty($k)){
					
					$data = M('Record')->where($map)->find();
					
					$departments = json_decode($data['department']);
					
					$this->assign('departments', $departments);
					
					$this->assign('k', $k);

				}
				
				$this->assign('tid', $tid);
				
				$department = json_decode($info[$nid]);
				
				$this->assign('department', $department);
				
			}elseif($nid == 'pic'){
				
				$this->assign('tid', $tid);
				
				$pics = str_replace('./', '', json_decode($info['pic']));
				
				foreach($pics as $k=>$v){
					
					$data[$k] = explode(".", $v);
					
					$pic[$k]['pic'] = $v;
					
					$pic[$k]['url'] = $data[$k][0];
					
					$pic[$k]['ext'] = $data[$k][1];
					
				}
				
				$this->assign('pic', $pic);
				
			}else{
				
				$this->assign('tid', $tid);
			
				$this->assign('scripts', $scripts);
				
			}
		
		}
		
		if($bid == 'scene'){
		
			$map['sid'] = $sid;
				
			$map['uid'] = $uid;
				
			$map['mid'] = $mid;
			
			$info = M('Scene')->find($id);
			
			if($nid == 'titles'){
				
				$pics = str_replace('./', '', json_decode($info['pic']));
				
				foreach($pics as $k=>$v){
					
					$data[$k] = explode(".", $v);
					
					$pic[$k]['pic'] = $v;
					
					$pic[$k]['url'] = $data[$k][0];
					
					$pic[$k]['ext'] = $data[$k][1];
					
				}

				$this->assign('pic', $pic);
				
				$hint = json_decode($info['hint']);
				
				$this->assign('hint', $hint);
				
				$this->assign('tid', $info['scenename']);
				
				$this->assign('scene', $info);

			}
			
			if($nid == 'department'){
				
				$department = json_decode($info[$nid]);
				
				$this->assign('department', $department);
				
				$this->assign('tid', $info['scenename']);
				
				$this->assign('scene', $info);
				
				$map['sid'] = $sid;
				
				$map['uid'] = $uid;
					
				$map['mid'] = $mid;
					
				$map['pid'] = $pid;
				
				$map['hid'] = $id;
				
				$infos = M('Hint')->where($map)->select();
				
				foreach($infos as $k=>$v){
				
					$infos['voice'] = str_replace('./', '', $infos['voice']);
				
				}
				
				$this->assign('hint', $infos);

			}
			
			if($nid == 'report'){
				
				$infoa = M('Scripts')->where(array('id'=>$sid))->find();
				
				$department = json_decode($infoa['department']);
				
				$this->assign('department', $department);
				
				$this->assign('tid', $infoa['scriptsname']);
				
				$this->assign('scene', $info);
				
				$map['sid'] = $sid;
				
				$map['uid'] = $uid;
					
				$map['mid'] = $mid;
					
				$map['pid'] = $pid;
				
				$infos = M('Report')->where($map)->select();
				
				foreach($infos as $k=>$v){
				
					$infos['voice'] = str_replace('./', '', $infos['voice']);
				
				}
				
				$this->assign('report', $infos);

			}
					
		}
		
		if($bid == 'record'){
			
			$map['sid'] = $sid;
			
			$map['uid'] = session(C('AUTH_KEYS'));
		
			$map['mid'] = session('gid');
			
			$map['pid'] = $pid;
			
			$info = M('Record')->where($map)->select();
			
			$report = $info[0]['report'];
			
		}
		
		$this->display();
		
	}
	
	public function scripts(){
		
		$id = I('get.id');
		
		$scripts = M('Scripts')->find($id);
		
		$this->assign('scripts', $scripts);
		
		$department = json_decode($scripts['department']);
		
		$this->assign('department', $department);
		
		$map['sid'] = $id;
		
		$map['uid'] = session(C('AUTH_KEYS'));
		
		$map['mid'] = session('gid');
		
		$pid = I('get.pid');
		
		$scene = M('Scene')->where($map)->order('id asc')->limit(0,1)->select();
		
		$cid = $scene[0]['id'];
		
		$this->assign('cid', $cid);
		
		$d = I('get.d');
		
		if($d){
			$this->redirect("Home/Ping/index"); 
		}
		
		$this->display();
		
	}
	
	public function voice(){
		
		header('Content-Type: audio/mpeg');
		
		$sid = I('get.sid');
		
		$pid = I('get.pid');
		
		$bid = I('get.bid');
		
		$nid = I('get.nid');
		
		$this->assign('nid', $nid);
		
		$tid = I('get.tid');
		
		$this->assign('tid', $tid);
		
		$k = I('get.k');
		
		if($bid == "scripts"){
		
			$info = M('Scripts')->where(array('id'=>$sid))->find();
			
			$text = $info[$nid];
		
		}
		
		if($bid == "scene"){
		
			$info = M('Scene')->where(array('id'=>$sid))->find();
			
			if($nid == "hint"){
				
				$hint = json_decode($info[$nid]);
				
				$text = implode(",",$hint);
				
			}else{
				
				$text = $info[$nid];
				
			}
		
		}
		
		//$url = "http://api.satori.moe/text2audio.php?text=$text";
		
		$text = urlencode(urlencode($text));
		$url = "http://tsn.baidu.com/text2audio?tex=$text&tok=24.6bbf3be1038b4c8cb5b851e159ed0064.2592000.1538233090.282335-11752112&lan=zh&cuid=08:00:27:9F:BD:99&ctp=1";
		$get = file_get_contents($url);
		
		echo $get;
		
	}
	
	
	
	public function acceptfile(){
		
		error_reporting(0);
		
    	try{
        	$name = time().rand(1000, 9999);
			
			$upload_path = './Public/voice/';
			
			if(!file_exists($upload_path)){
				
				mkdir($upload_path);
				
			}
		
			$upload_filename =	$upload_path.$name;
        
			$fp = fopen($upload_filename . '.mp3', "wb");

        	fwrite($fp, file_get_contents('php://input'));

        	fclose($fp);

        	exit($name . ".mp3");
			
    	}catch (Exception $e) {
			
        	exit;
			
    	}
		
	}
	
	public function start(){
		
		$sid = I('get.sid');
		
		$pid = I('get.pid');
		
		$time = I('get.time');
		
		$m = array(
			'sid' => $sid,
			'uid' => session(C('AUTH_KEYS')),
			'mid' => session('gid'),
			'pid' => $pid,
		);
			
		$data['start_time'] = $time;
			
		$res = M('Record')->where($m)->save($data);
		
		if($res){
					
			echo json_encode(array('info'=>'操作成功', 'status'=>'y'));
					
		}else{
					
			$error = M('Record')->getError();
					
			echo json_encode(array('info'=>$error, 'status'=>'n'));
					
		}
			
	}
		
	public function ends(){
		
		$sid = I('get.sid');
		
		$pid = I('get.pid');
		
		$time = I('get.time');
		
		$m = array(
			'sid' => $sid,
			'uid' => session(C('AUTH_KEYS')),
			'mid' => session('gid'),
			'pid' => $pid,
		);
			
		$data['end_time'] = $time;
			
		$res = M('Record')->where($m)->save($data);
		
		if($res){
					
			echo json_encode(array('info'=>'操作成功', 'status'=>'y'));
					
		}else{
					
			$error = M('Record')->getError();
					
			echo json_encode(array('info'=>$error, 'status'=>'n'));
					
		}
		
	}
	
	public function reloads(){
		
		$sid = I('get.sid');
		
		$pid = I('get.pid');
		
		$m = array(
			'sid' => $sid,
			'uid' => session(C('AUTH_KEYS')),
			'mid' => session('gid'),
			'pid' => $pid,
		);
			
		$data['start_time'] = '';
		
		$data['end_time'] = '';
			
		$res = M('Record')->where($m)->save($data);
		
		if($res){
					
			echo json_encode(array('info'=>'操作成功', 'status'=>'y'));
					
		}else{
					
			$error = M('Record')->getError();
					
			echo json_encode(array('info'=>$error, 'status'=>'n'));
					
		}
		
	}
	
	public function starts(){
		
		$sid = I('get.sid');
		
		$pid = I('get.pid');
		
		$m = array(
			'sid' => $sid,
			'uid' => session(C('AUTH_KEYS')),
			'mid' => session('gid'),
			'pid' => $pid,
		);
			
		$res = M('Record')->where($m)->select();
		
		$time = time();
		
		if($res){
					
			echo json_encode(array('info'=>'操作成功', 'status'=>'y', 'time'=>$time));
					
		}else{
					
			$error = M('Record')->getError();
					
			echo json_encode(array('info'=>$error, 'status'=>'n'));
					
		}
		
	}
	
}
